import { Vue, Component } from 'vue-property-decorator'
	import PageContainer from '@/moduleC/components/layout/PageContainer.vue'
	import { OrderItem, OrderStatus } from '@/moduleC/types/order'
	import { api } from '@/moduleC/api'
	import store from '@/moduleC/store'

	@Component({
		components: {
			PageContainer
		}
	})
	export default class OrderPage extends Vue {
		private orderList : OrderItem[] = []
		private loading = false
		private hasMore = true
		private page = 1
		private pageSize = 10
		readonly OrderStatus = OrderStatus

		created() {
			this.loadData()
		}
		onShow(){
			const orderIdx:number = store.state.orderAdd;
			const orderList:any = store.state.orderDetail;
			if (orderIdx && orderIdx >= 0) {
			  const list = [...this.orderList]
			  list.forEach(el => {
			    if (Number(el.orderNo) === Number(orderIdx)) el.status = 2
			  })
			  this.orderList = list
			}
		}
		getStatusText(status : OrderStatus) : string {
			switch (status) {
				case OrderStatus.COMPLETED:
					return '已完成'
				case OrderStatus.PENDING_COMMENT:
					return '待评论'
				case OrderStatus.COMMENTED:
					return '已评论'
				default:
					return ''
			}
		}

		async loadMore() {
			if (this.loading || !this.hasMore) return
			this.page++
			await this.loadData()
		}

		async refresh() {
			this.page = 1
			this.orderList = []
			this.hasMore = true
			await this.loadData()
		}

		private async loadData() {
			this.loading = true
			try {
				const res = await api.getOrderList({
					page: this.page,
					pageSize: this.pageSize
				})

				if (res.code === 0) {
					const newList = res.data.list
					if (this.page === 1) {
						this.orderList = newList
					} else {
						this.orderList = [...this.orderList, ...newList]
					}
					this.hasMore = res.data.hasMore
				}
				console.log(this.orderList)
			} finally {
				this.loading = false
			}
		}

		gotoDetails(item : OrderItem,idx: number) {
			store.dispatch('setStoreData',{
				_key: 'orderDetail',
				_val: item
			});
			// console.log(store.state.orderDetail	)
		
		// 	console.log(store.state.orderDetail)
			uni.navigateTo({
				url: `/moduleC/pages/order/details`
			})
		}

		gotoComment(item : OrderItem) {
			store.dispatch('setStoreData',{
				_key: 'orderDetail',
				_val: item
			});
			// uni.setStorageSync('_orderInfo', item)
			uni.navigateTo({
				url: '/moduleC/pages/comment/add'
			})
		}
	}